﻿@using DormMS.Model.Enum
@{
    Layout = "";
}

<h3>抽奖活动管理</h3>

<div class="panel panel-default" ng-controller="DrawManageCtrl" ng-class="{ 'whirl standard': isLoading,'': isLoading }">
    <div class="panel-heading">
        <div class="form-inline ng-pristine ng-valid">
            <div class="form-group" style="width:100%;">
                <input style="width: 15%;" placeholder="关键字" class="form-control" type="text" ng-model="Keywords">

                <div class="input-group">
                    <input readonly="readonly" placeholder="开始时间(最小时间)" style="z-index: 0;" type="text" class="form-control" datetime-picker="yyyy-MM-dd HH:mm" ng-model="StartTimeFirst"
                           is-open="picker1.open" datepicker-options="datepickerOptions" />
                    <span class="input-group-btn">
                        <button type="button" class="btn btn-default" ng-click="openCalendar($event, 'picker1')"><i class="fa fa-calendar"></i></button>
                    </span>
                </div>
                ～
                <div class="input-group">
                    <input readonly="readonly" style="z-index: 0;" placeholder="开始时间(最大时间)" type="text" class="form-control" datetime-picker="yyyy-MM-dd HH:mm" ng-model="StartTimeSec"
                           is-open="picker2.open" datepicker-options="datepickerOptions" />
                    <span class="input-group-btn">
                        <button type="button" class="btn btn-default" ng-click="openCalendar($event, 'picker2')"><i class="fa fa-calendar"></i></button>
                    </span>
                </div>

                <select ng-model="IsActive" class="form-control">
                    <option value="0">全部</option>
                    <option value="1">有效</option>
                    <option value="2">已过时</option>
                </select>
                <button type="submit" class="btn btn-labeled btn-primary" ng-click="tableParams.$params.page = 1;tableParams.reload()">
                    <span class="btn-label">
                        <em class="fa fa-search"></em>
                    </span>搜索
                </button>
                <button type="submit" class="btn btn-labeled btn-success" ng-click="updateOrCreateDraw(0)">
                    <span class="btn-label">
                        <em class="fa fa-plus"></em>
                    </span>新增
                </button>
            </div>
        </div>
    </div>
    <div class="panel-body">
        <div class="table-responsive">
            <table ng-table="tableParams" class="table table-striped table-bproducted table-hover">
                <colgroup>
                    <col style="width: 15%" />
                    <col style="width: 8%" />
                    <col style="width: 8%" />
                    <col style="width: 8%" />
                    <col style="width: 8%" />
                    <col style="width: 8%" />
                    <col style="width: 10%" />
                    <col style="width: 10%" />
                    <col style="width: 10%" />
                    <col style="width: 15%" />
                </colgroup>
                <tbody>
                    <tr ng-repeat="item in $data">
                        <td data-title="'活动名'" class="text-left">{{item.DrawName}}</td>
                        <td data-title="'消耗积分'" sortable="'ConsumePoints'" class="text-center">{{item.ConsumePoints}}</td>
                        <td data-title="'抽奖限制次数'" sortable="'Chances'" class="text-center">{{item.ChancesM}}</td>
                        <td data-title="'奖项数量'" sortable="'PrizeCount'" class="text-center">{{item.PrizeCount}}</td>
                        <td data-title="'中奖人数'" sortable="'GetPersonCount'" class="text-center">{{item.GetPersonCount}}</td>
                        <td data-title="'排序'" sortable="'Sort'" class="text-center">{{item.Sort}}</td>
                        <td data-title="'添加时间'" sortable="'AddTime'" class="text-center">{{item.AddTime}}</td>
                        <td data-title="'活动开始时间'" sortable="'StartTime'" class="text-center">{{item.StartTime}}</td>
                        <td data-title="'活动过期时间'" sortable="'EndTime'" class="text-center">{{item.EndTime}}</td>                        
                        <td data-title="'操作'" nowrap="nowrap" class="text-center">
                            <button type="button" ng-click="updateStatus($index,item.Id,1)" class="btn btn-sm btn-warning" ng-show="!item.IsDisplay" title="上架">
                                <em class="fa fa-hand-o-up"></em>
                            </button>
                            <button type="button" ng-click="updateStatus($index,item.Id,0)" class="btn btn-sm btn-warning" ng-show="item.IsDisplay" title="下架">
                                <em class="fa fa-hand-o-down"></em>
                            </button>
                            <button type="button" ng-click="updateOrCreateDraw(item.Id)" class="btn btn-sm btn-info" ng-show="!item.IsDisplay" title="编辑活动信息">
                                <em class="fa fa-edit"></em>
                            </button>
                            <button type="button" ng-click="editDrawReward(item.Id)" class="btn btn-sm btn-success" ng-show="!item.IsDisplay" title="编辑奖项">
                                <em class="fa fa-edit"></em>
                            </button>
                            <button type="button" ng-click="showRecordList(item.Id)" class="btn btn-sm btn-info" title="查看中奖明细">
                                <em class="fa fa-eye"></em>
                            </button>
                            <button ng-click="deleteDraw(item.Id)" type="button" class="btn btn-sm btn-danger" ng-show="!item.IsDisplay" title="删除">
                                <em class="fa fa-trash-o"></em>
                            </button>
                        </td>
                    </tr>
                </tbody>
            </table>
        </div>
    </div>
    <script type="text/ng-template" id="modalDialogId">
        <div class="ngdialog-message text-center">
            <h3>确定要删除吗?</h3>
            <p>删除后不可恢复!</p>
            <br />
            <button type="button" ng-click="confirm()" class="btn btn-primary mr">确认</button>
            <button type="button" ng-click="closeThisDialog('button')" class="btn btn-default">取消</button>
        </div>
    </script>
</div>

<script>
    App.controller('DrawManageCtrl', [
    '$scope', '$resource', '$state', '$route', 'ngDialog', '$http', 'ngTableParams', 'ngTableDataService', 'Notify', '$modal', "$filter", "$timeout", "$stateParams", "$location",
    function ($scope, $resource, $state, $route, ngDialog, $http, ngTableParams, ngTableDataService, Notify, $modal, $filter, $timeout, $stateParams, $location) {
        'use strict';
        $scope.IsLoading = true;
        $scope.PageDetailList = [];
        $scope.tableParams = new ngTableParams({
            page: 1,
            count: 10,
            sorting: {
                //Id: 'desc'     // 初始化排序
            }
        }, {
            getData: function ($defer, params) {
                $scope.isLoading = true;
                var requestParams = {
                    Skip: (params.page() - 1) * params.count(),
                    Take: params.count(),
                    Keywords: $scope.Keywords,
                    IsActive: $scope.IsActive,
                    StartTimeFirst: $scope.StartTimeFirst,
                    StartTimeSec: $scope.StartTimeSec
                };
                $http.post('/Draw/DrawList', ngTableDataService.getServerSideParams(params.orderBy().toString(), params, requestParams))
                    .success(function (data, status) {

                        params.total(data.Total);
                        $defer.resolve(data.Results);

                        //记录查询参数，后续返回使用
                        $scope.urlParams = {
                            page: params.page(),
                            Keywords: $scope.Keywords,
                            //FirstCategoryId: $scope.FirstCategoryId,
                            //SecondCategoryId: $scope.SecondCategoryId,
                            IsDisplay: $scope.IsDisplay
                        }
                        // $state.go($state.current, urlParams, { location: true, notify: false, reload: false });
                        //$location.search('page', params.page());
                        //$state.transitionTo('app.normalSaleProduct', urlParams, {
                        //    location: true,
                        //    inherit: true,
                        //    relative: $state.$current,
                        //    notify: false
                        //});

                        $scope.isLoading = false;
                    });


            }
        });

        //如果通过链接传值则对参数做一次初始化修改
        $scope.tableParams.page($stateParams.page ? $stateParams.page : 1);
        $scope.Keywords = $stateParams.Keywords ? $stateParams.Keywords : undefined;
        $scope.IsActive = $stateParams.IsActive ? $stateParams.IsActive : 0;


        //编辑添加用同一个方法处理，参数传0则为添加
        $scope.updateOrCreateDraw = function (id) {
            $scope.urlParams.Id = id;
            $state.go('app.drawEdit', $scope.urlParams);
        }

        //进入编辑奖项页面
        $scope.editDrawReward = function (id) {
            $scope.urlParams.Id = id;
            $state.go('app.drawRewardEdit', $scope.urlParams);
        }

        //上下架操作
        $scope.updateStatus = function (index, id, status) {
            $http.post("/Draw/ModifyDrawStatus", { id: id, isDisplay: status }).then(function (returnData) {
                if (returnData.data.isSuccess) {
                    $scope.tableParams.reload();
                    Notify.alert(returnData.data.msg, { status: 'success', timeout: 1000 });
                } else {
                    Notify.alert(returnData.data.msg, { status: 'warning', timeout: 1000 });
                }
            });
        }

        //中奖纪录页
        $scope.showRecordList = function (id) {
            $state.go('app.drawRecordList', { Id: id });
        }

        //删除操作
        $scope.deleteDraw = function (id) {
            ngDialog.openConfirm({
                template: 'modalDialogId',
                className: 'ngdialog-theme-default'
            }).then(function (value) {
                $http.post("/Draw/DeleteDraw", { id: id }).then(function (returnData) {
                    if (returnData.data.isSuccess) {
                        $scope.tableParams.reload();
                        Notify.alert(returnData.data.msg, { status: 'success', timeout: 1000 });
                    } else {
                        Notify.alert(returnData.data.msg, { status: 'warning', timeout: 1000 });
                    }
                });
            }, function (reason) {
            }
            );
        }        
        // 日期插件
        $scope.picker1 = {
            date: new Date()
        };

        $scope.picker2 = {
            date: new Date()
        };

        $scope.datepickerOptions = {
            showWeeks: false,
            startingDay: 1,
            dateDisabled: function (data) {
                //return (data.mode === 'day' && (new Date().toDateString() == data.date.toDateString()));
            }
        }
        $scope.openCalendar = function (e, picker) {
            $scope[picker].open = true;
        };

    }])



</script>